package filter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import error.GedivaException;

public class OneValueFilter extends Filter<String, List<Double>>
{
    private double minValue;
    private double maxValue;

    public OneValueFilter()
    {
        super("OneValueFilter");
    }

    public String toString()
    {
        return "Truncate magnitude";
    }

    public Map<Object, List<Object>> filterData(
            Map<Object, List<Object>> inputData)
    {
        try
        {
            Map<Object, List<Object>> outputData = new HashMap<Object, List<Object>>(
                    inputData);
            double firstVal = Double.parseDouble(getStoredConstraints().get(0));
            double secondVal = Double
                    .parseDouble(getStoredConstraints().get(1));
            minValue = Math.min(firstVal, secondVal);
            maxValue = Math.max(firstVal, secondVal);
            for (Object s : inputData.keySet())
            {
                double barHeight = (Double) outputData.get(s).get(0);
                if (barHeight < minValue || barHeight > maxValue)
                    outputData.remove(s);
            }
            return outputData;
        }
        catch (NumberFormatException e)
        {
            throw new GedivaException("the max and min values must be numbers");
        }
    }
}
